package org.example.design.memo;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @Classname Test
 * @Description 备忘录模式测试类
 * @Date 2020/11/10 3:23 下午
 * @Created by xiangjd
 */
public class Test {

    private Logger logger = LoggerFactory.getLogger(Test.class);

    @org.junit.Test
    public void test(){
        Admin admin = new Admin();
        ConfigOriginator configOriginator = new ConfigOriginator();
        configOriginator.setConfigFile(new ConfigFile("1000001", "配置内容A=哈哈", new Date(), "小傅哥"));
        //保存配置
        admin.append(configOriginator.saveMemento());
        configOriginator.setConfigFile(new ConfigFile("1000002", "配置内容A=嘻嘻", new Date(), "小傅哥"));
        //保存配置
        admin.append(configOriginator.saveMemento());
        configOriginator.setConfigFile(new ConfigFile("1000003", "配置内容A=哟哟", new Date(), "小傅哥"));
        //保存配置
        admin.append(configOriginator.saveMemento());
        configOriginator.setConfigFile(new ConfigFile("1000004", "配置内容A=思思", new Date(), "小傅哥"));
        //保存配置
        admin.append(configOriginator.saveMemento());

        //历史配置（回滚）
        configOriginator.getMemento(admin.undo());
        logger.info("历史配置(回滚)undo: {}", JSON.toJSONString(configOriginator.getConfigFile()));


        //历史配置（回滚）
        configOriginator.getMemento(admin.undo());
        logger.info("历史配置(回滚)undo: {}", JSON.toJSONString(configOriginator.getConfigFile()));

        //历史配置(前进)
        configOriginator.getMemento(admin.redo());
        logger.info("历史配置(前进)redo: {}", JSON.toJSONString(configOriginator.getConfigFile()));

        //历史配置(获取)
        configOriginator.getMemento(admin.get("1000002"));
        logger.info("历史配置(获取)get: {}", JSON.toJSONString(configOriginator.getConfigFile()));

    }
}
